<?php
class Admini_AgentController extends Uuch_ManageController {

    public function init() {
        parent::init();
        $this->appendCss('/css/home/warehouse.css')->appendCss('/js/boxy/css/boxy.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/js/jquery.min.js');
        $this->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')
                ->appendScript('/js/uuch.js')->appendScript('/js/boxy/js/jquery.boxy.js');

        $this->appendCss('/admins/css/adminshare.css');
        $this->appendCss('/admins/css/cangku.css');
        $this->view->type = 'agent';
    }

    
    public function customerAction(){
        
        $this->setTitle("推荐客户列表");
        $pagesize = $this->getSafeParam("pagesize");
        $pagesize = $pagesize ? $pagesize : 15;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $referrerId = '45824';
        $nums = '';
        $result = Application_Model_Agent::getFilter($referrerId, $pagesize, $pageindex);
        $this->view->stype = 1;
        $this->view->userlist = $result["list"];
        $this->view->userall = $result;
    }
    
    public function waybillAction() {
        $this->setTitle("推荐客户运单");
        $pagesize = $this->getSafeParam("pagesize");
        $pagesize = $pagesize ? $pagesize : 15;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $status = $this->getSafeParam('status');
        $quick = $this->getSafeParam('quick');
        $user_ids = '';
        $where = array();
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
        $db = $bootstrap->getResource('db');
        $userlist = $db->fetchAll(
             "SELECT id FROM uuch_user where referrerId = '45824'"
         );
        foreach ($userlist as $k => $value) {
             $user_ids .= $value['id'].',';
        }
        //$user_ids = rtrim($user_ids,',');
        unset($userlist);
        $user_ids .=45824;
        $where[" userId in "] = "(".$user_ids.")";
        //$where[" and stockDate!= "] = '';
        if ($status =='1') {
             $where[" and  interior < "]= 11;
         }
        if ($status == '2') {
             $where[" and  interior >= "]= 11;
         }
         $where[" and  dateCreated >= "]= '2016-06-01';
        if ($quick) {
            if($status == '2'){
                $where[" and stockDate like "] = "%".$quick."%";
            }else{
                $where[" and dateCreated like "] = "%".$quick."%";
            }
         }
        $billall = Application_Model_wayBillItem::getBill_All($where, $pagesize, $pageindex);
        $billalllist = $billall['list'];
        $dq_time = date('Y-m',time());
        $dq_zk = $db->fetchAll("SELECT count(w.id) AS nums,sum(IFNULL(w.lastmoney,0)+
IFNULL(f2.money,0)+IFNULL(f3.money,0)+IFNULL(f4.money,0)+IFNULL(f5.money,0)+
IFNULL(f7.money,0)+IFNULL(f8.money,0)+IFNULL(f9.money,0)+IFNULL(f10.money,0)+
IFNULL(f6.money,0)) AS money FROM uuch_waybill AS w 
LEFT JOIN uuch_wayFee as f2 on w.id = f2.wayId and f2.feeType = 2
LEFT JOIN uuch_wayFee as f3 on w.id = f3.wayId and f3.feeType = 3
LEFT JOIN uuch_wayFee as f4 on w.id = f4.wayId and f4.feeType = 4
LEFT JOIN uuch_wayFee as f5 on w.id = f5.wayId and f5.feeType = 5
LEFT JOIN uuch_wayFee as f6 on w.id = f6.wayId and f6.feeType = 6
LEFT JOIN uuch_wayFee as f7 on w.id = f7.wayId and f7.feeType = 7
LEFT JOIN uuch_wayFee as f8 on w.id = f8.wayId and f8.feeType = 19
LEFT JOIN uuch_wayFee as f9 on w.id = f9.wayId and f9.feeType = 20 
LEFT JOIN uuch_wayFee as f10 on w.id = f10.wayId and f10.feeType = 21
where w.stockDate like '%$dq_time%' and w.userId in ($user_ids) ");
        $this->view->dq_money = $dq_zk[0]['money'];
        $this->view->dq_nums = $dq_zk[0]['nums'];
        $this->view->status = $status;
        $this->view->stype = 2;
        $this->view->quick = $quick;
        $this->view->status = $status;
        $this->view->billall = $billall;
        $this->view->billlist = $billalllist;
        
    }


    /**
     * 月度分成
     */
    public function commissionAction() {
        $this->setTitle("月度分成");
        $pagesize = $this->getSafeParam("pagesize");
        $pagesize = $pagesize ? $pagesize : 15;
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $user_ids = '';
        $where = array();
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
        $db = $bootstrap->getResource('db');
        $userlist = $db->fetchAll(
             "SELECT id FROM uuch_user where referrerId = '45824'"
         );
        foreach ($userlist as $k => $value) {
             $user_ids .= $value['id'].',';
        }
        //$user_ids = rtrim($user_ids,',');
        $user_ids .=45824; 
        unset($userlist);
        $commissionlist = $db->fetchAll(
             "SELECT DATE_FORMAT(stockDate,'%Y-%m') AS month FROM  uuch_waybill where stockDate!='' and userId in ($user_ids)  group by month"
         );
        $size = count($commissionlist);
        $commissionlist = $db->fetchAll("SELECT DATE_FORMAT(w.stockDate,'%Y-%m') AS month,sum(IFNULL(w.lastmoney,0)+
IFNULL(f2.money,0)+IFNULL(f3.money,0)+IFNULL(f4.money,0)+IFNULL(f5.money,0)+
IFNULL(f7.money,0)+IFNULL(f8.money,0)+IFNULL(f9.money,0)+IFNULL(f10.money,0)+
IFNULL(f6.money,0)) AS money FROM  uuch_waybill AS w 
LEFT JOIN uuch_wayFee as f2 on w.id = f2.wayId and f2.feeType = 2
LEFT JOIN uuch_wayFee as f3 on w.id = f3.wayId and f3.feeType = 3
LEFT JOIN uuch_wayFee as f4 on w.id = f4.wayId and f4.feeType = 4
LEFT JOIN uuch_wayFee as f5 on w.id = f5.wayId and f5.feeType = 5
LEFT JOIN uuch_wayFee as f6 on w.id = f6.wayId and f6.feeType = 6
LEFT JOIN uuch_wayFee as f7 on w.id = f7.wayId and f7.feeType = 7
LEFT JOIN uuch_wayFee as f8 on w.id = f8.wayId and f8.feeType = 19
LEFT JOIN uuch_wayFee as f9 on w.id = f9.wayId and f9.feeType = 20 
LEFT JOIN uuch_wayFee as f10 on w.id = f10.wayId and f10.feeType = 21
where w.stockDate!='' and dateCreated >= '2016-06-01' and w.userId in ($user_ids)  group by month  order by month desc LIMIT $pageindex, $pagesize");
 
        $pages = intval($size / $pagesize);
        $pages = $pages + ($size % $pagesize != 0 ? 1 : 0);
        $index = $pageindex / $pagesize;

        $commissionAll = array('size' => $size, 'list' => $commissionlist, 'pages' => $pages, 
        'index' => $index, 'pagesize' => $pagesize);
        $this->view->stype = 3;
        $this->view->commissionAll = $commissionAll;
        $this->view->commissionlist = $commissionlist;

    }

}
